package com.yc.http.tomcat.impl;

import com.yc.http.tomcat.HttpSession;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * WEB会话: 服务器端保存用户数据的技术
 */
public class HttpSessionImpl implements HttpSession {
    // 创建时间戳
    private long creationTime;
    // 唯一标识
    private String id;
    // 最后访问时间
    private long lastAccessedTime;
    // 最长活动时间  Tomcat 的会话有效时间 30 分钟
    private int maxInactiveInterval;
    // 属性集合
    private Map<String,Object> attributeMap = new HashMap<>();

    public HttpSessionImpl(){
        creationTime = System.currentTimeMillis();
        id = UUID.randomUUID().toString().replaceAll("-","");
    }

    @Override
    public long getCreationTime() {
        return creationTime;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public long getLastAccessedTime() {
        return lastAccessedTime;
    }

    @Override
    public int getMaxInactiveInterval() {
        return maxInactiveInterval;
    }

    @Override
    public void invalidate() {

    }

    @Override
    public void setAttribute(String name, Object value) {
        attributeMap.put(name,value);
    }

    @Override
    public Object getAttribute(String name) {
        return attributeMap.get(name);
    }

    @Override
    public void removeAttribute(String name) {
        attributeMap.remove(name);
    }
}
